package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.goods.entity.goods.PublicProperties;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.goods.GoodsBrandMapper;
import com.hoshiicloud.goods.mapper.goods.PublicPropertiesMapper;
import com.hoshiicloud.goods.service.PublicPropertiesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 03:29 2019/7/26
 * @PACKAGENAME com.hoshiicloud.goods.service.impl
 * @Description
 */
@Service
public class PublicPropertiesServiceImpl extends ServiceImpl<PublicPropertiesMapper, PublicProperties> implements PublicPropertiesService {
    @Autowired
    private IdGeneratorService idGeneratorService;


    /**
     * 获取分页公共属性信息
     * @param page
     * @param queryWrapper
     * @param key
     * @return
     */
    @Cacheable(cacheNames = "PublicProperties", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品域-公共属性表-获取公共属性列表")
    @Override
    public Page<PublicProperties> getPage(Page<PublicProperties> page, QueryWrapper<PublicProperties> queryWrapper, String key) {
        baseMapper.selectPage(page,queryWrapper);
         for(PublicProperties properties :page.getRecords()){
             if("规格类型".equals(properties.getPropertyName())){
                 properties.setPropertyValue(properties.getPropertyValue().replace("颜色,",""));
             }
         }
        return page;
    }


    @Override
    public PublicProperties getById(Long id) {
        PublicProperties p = baseMapper.selectById(id);
        if("规格类型".equals(p.getPropertyName())){
            p.setPropertyValue(p.getPropertyValue().replace("颜色,",""));
        }
        return p;
    }


    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PublicProperties",allEntries = true)
    @Override
    public boolean save(PublicProperties publicProperties) {
        if("规格类型".equals(publicProperties.getPropertyName())){
            publicProperties.setPropertyValue("颜色,"+publicProperties.getPropertyValue());
        }
        String id=idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)){
            throw new ServiceException(CommonError.error("新增失败，id生成失败"));
        }
        publicProperties.setId(Long.valueOf(id));
        publicProperties.setCreatedTime(LocalDateTime.now());
        publicProperties.setDeleted(0);
        int row=baseMapper.insert(publicProperties);
        if (row<=0){
            throw new ServiceException(CommonError.error("新增公共属性失败"));
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PublicProperties",allEntries = true)
    @Logable(authName = "商品域-公共属性表-更新公共属性")
    @Override
    public boolean updateById(PublicProperties publicProperties) {
        PublicProperties entity = this.getById(publicProperties.getId());
        if("规格类型".equals(entity.getPropertyName())){
            publicProperties.setPropertyValue("颜色,"+publicProperties.getPropertyValue());
        }
        publicProperties.setUpdatedTime(LocalDateTime.now());
        int row=baseMapper.updateById(publicProperties);
        if (row<=0){
            throw new ServiceException(CommonError.error("公共属性更新失败"));
        }
        return false;
    }


    @Override
    @Cacheable(cacheNames = "PublicProperties", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品域-公共属性表-获取公共属性下拉数据")
    public List<String> queryDropdownBox(Long companyId, String propertyName, String key){
        QueryWrapper<PublicProperties> queryWrapper = new QueryWrapper<PublicProperties>();
        queryWrapper.lambda()
                .eq(PublicProperties::getCompanyId, companyId)
                .eq(PublicProperties::getPropertyName,propertyName);
        PublicProperties pro = this.getOne(queryWrapper);
        if(null == pro){
            return new ArrayList<>();
        }else{
            return Arrays.asList(pro.getPropertyValue().split(","));
        }

    }

}
